package com.yanqu.road.server.gameplayer.module.activity.ad;

import com.yanqu.road.entity.activity.ad.AdActivityUserData;
import com.yanqu.road.entity.activity.ad.WatchTimesReward;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.bussiness.activity.AdActivityBussiness;
import com.yanqu.road.pb.ad.AdActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ad.AdActivityConfig;
import com.yanqu.road.server.manger.activity.ad.AdActivityMgr;
import com.yanqu.road.server.pb.AdActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

public class AdActivityModule extends GeneralModule {
    private AdActivityUserData adActivityUserData;

    public AdActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        AdActivityConfig adActivityConfig = AdActivityMgr.getAdActivityConfig();
        if(null != adActivityConfig) {
            adActivityUserData = AdActivityBussiness.getAdActivityUserData(adActivityConfig.getActivityId(), player.getUserId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if(null != adActivityUserData){
            if(adActivityUserData.isInsertOption()){
                AdActivityBussiness.addAdActivityUserData(adActivityUserData);
            }else if(adActivityUserData.isUpdateOption()){
                AdActivityBussiness.updateAdActivityUserData(adActivityUserData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        initUserData();
        checkFirstRewardFinish(adActivityUserData);
        syncUserData();
    }

    public void initUserData(){
        AdActivityConfig adActivityConfig = AdActivityMgr.getAdActivityConfig();
        if(null != adActivityConfig){
            initUserData(adActivityConfig.getActivityId());
        }
    }

    private synchronized void initUserData(int activityId){
        if(null == adActivityUserData || adActivityUserData.getActivityId() != activityId){
            AdActivityUserData userData = new AdActivityUserData();
            userData.setActivityId(activityId);
            userData.setUserId(player.getUserId());
            userData.setDailyResetTime(LocalDateTimeHelper.getZeroTimeTimeStamp());
            userData.setInsertOption();
            adActivityUserData = userData;
        }
    }

    public void dailyReset(){
        if(null == adActivityUserData){
            return;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        boolean needSync = false;
        if (adActivityUserData.getDailyResetTime() < zeroTime) {
            synchronized (adActivityUserData) {
                if(adActivityUserData.getDailyWatchTimes() > 0){
                    needSync = true;
                }
                adActivityUserData.setDailyWatchTimes(0);
                adActivityUserData.setDailyResetTime(zeroTime);
            }
        }
        if(needSync){
            syncUserData();
        }
    }

    public void syncUserData(){
        if(null == adActivityUserData){
            return;
        }
        AdActivityConfig adActivityConfig = AdActivityMgr.getAdActivityConfig(adActivityUserData.getActivityId());
        if(null == adActivityConfig){
            return;
        }
        AdActivityProto.AdUserDataMsg.Builder dataMsg = AdActivityPb.parseAdUserDataMsg(adActivityUserData);
        if(null != dataMsg){
            player.sendPacket(Protocol.U_AD_USER_DATA_SYNC, dataMsg);
        }
    }

    public int watchFinishReward(int activityId, boolean getUnlockReward){
        AdActivityConfig adActivityConfig = AdActivityMgr.getAdActivityConfig(activityId);
        if(null == adActivityConfig){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(null == adActivityUserData){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(getUnlockReward){
            if(adActivityUserData.isGetUnlockReward()){
                return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            }
            adActivityUserData.setGetUnlockReward(true);
        }else {
            long nowTime = System.currentTimeMillis();
            if (nowTime - adActivityUserData.getLastWatchTime() < adActivityConfig.getCoolCdTime() * 1000) {
                return GameErrorCode.E_AD_ACTIVITY_IN_WATCH_CD_TIME;
            }
            if (adActivityUserData.getDailyWatchTimes() >= adActivityConfig.getDailyMaxWatchTimes()) {
                return GameErrorCode.E_AD_ACTIVITY_DAILY_WATCH_TIME_MAX;
            }
            //设置观看时间
            adActivityUserData.setLastWatchTime(nowTime);
            //加观看次数
            synchronized (adActivityUserData) {
                dailyReset();
                adActivityUserData.setDailyWatchTimes(adActivityUserData.getDailyWatchTimes() + 1);
                if (adActivityUserData.getSecondTotalWatchTimes() > 0) {
                    adActivityUserData.setSecondTotalWatchTimes(adActivityUserData.getSecondTotalWatchTimes() + 1);
                } else {
                    if (adActivityUserData.getFirstTotalWatchTimes() >= adActivityConfig.getFirstMaxWatchTimes()) {
                        adActivityUserData.setSecondTotalWatchTimes(adActivityUserData.getSecondTotalWatchTimes() + 1);
                    } else {
                        adActivityUserData.setFirstTotalWatchTimes(adActivityUserData.getFirstTotalWatchTimes() + 1);
                    }
                }
            }
        }
        //加奖励
        Property reward = adActivityConfig.getSingleReward();
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.AdActivitySingleReward);
        syncUserData();
        AdActivityProto.WatchFinishRewardRespMsg.Builder respMsg = AdActivityProto.WatchFinishRewardRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
        player.sendPacket(Protocol.U_AD_WATCH_FINISH_REWARD, respMsg);
        return 0;
    }

    public int getWatchTimesReward(int activityId, int index){
        AdActivityConfig adActivityConfig = AdActivityMgr.getAdActivityConfig(activityId);
        if(null == adActivityConfig){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(null == adActivityUserData){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        String reward = "";
        if(!adActivityUserData.isFirstRewardFinish()){//领第一轮奖励
            if(adActivityUserData.isInFirstRewardList(index)){//已经领过了
                return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            }
            WatchTimesReward timesReward = adActivityConfig.getFirstWatchTimesReward(index);
            if(null == timesReward){
                return GameErrorCode.E_AD_ACTIVITY_WATCH_TIMES_REWARD_INDEX_ERROR;
            }
            if(timesReward.getTimes() > adActivityUserData.getFirstTotalWatchTimes()){
                return GameErrorCode.E_AD_ACTIVITY_WATCH_TIMES_NO_ENOUGH;
            }
            adActivityUserData.addFirstReward(index);
            //加奖励
            reward = timesReward.getReward();
            player.getModule(CurrencyModule.class).addCurrency(timesReward.getReward(), eLogMoneyType.Activity, eLogMoneyType.AdActivityWatchTimesReward);
            //检测第一轮是否领完
            checkFirstRewardFinish(adActivityUserData);
        }else {//领第二轮奖励
            if(adActivityUserData.isInSecondRewardList(index)){//已经领过了
                return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            }
            WatchTimesReward timesReward = adActivityConfig.getSecondWatchTimesReward(index);
            if(null == timesReward){
                return GameErrorCode.E_AD_ACTIVITY_WATCH_TIMES_REWARD_INDEX_ERROR;
            }
            if(timesReward.getTimes() > adActivityUserData.getSecondTotalWatchTimes()){
                return GameErrorCode.E_AD_ACTIVITY_WATCH_TIMES_NO_ENOUGH;
            }
            adActivityUserData.addSecondReward(index);
            //加奖励
            reward = timesReward.getReward();
            player.getModule(CurrencyModule.class).addCurrency(timesReward.getReward(), eLogMoneyType.Activity, eLogMoneyType.AdActivityWatchTimesReward);
        }
        syncUserData();
        AdActivityProto.WatchTimesRewardRespMsg.Builder respMsg = AdActivityProto.WatchTimesRewardRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setReward(reward);
        player.sendPacket(Protocol.U_AD_WATCH_TIMES_REWARD, respMsg);
        return 0;
    }

    private void checkFirstRewardFinish(AdActivityUserData userData){
        if(null == userData){
            return;
        }
        AdActivityConfig adActivityConfig = AdActivityMgr.getAdActivityConfig(userData.getActivityId());
        if(null == adActivityConfig){
            return;
        }
        if(userData.isFirstRewardFinish()){
            return;
        }
        int firstRewardSize = adActivityConfig.getFirstRewardListSize();
        boolean isFinish = true;
        for(int i = 0; i < firstRewardSize; i++){
            if(!userData.isInFirstRewardList(i)){
                isFinish = false;
                break;
            }
        }
        if(isFinish){
            userData.setFirstRewardFinish(isFinish);
        }
    }
}
